﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestLink2Net.Bal.Interfaces;
using TestLink2Net.Entities;
using TestLink2Net.Dal;

namespace TestLink2Net.Bal
{
    public class SecurityMster : ISecurityMaster
    {
        private SecurityMster()
        {
        }

        public static ISecurityMaster Instance
        {
            get
            {
                return new SecurityMster();
            }
        }



        #region ISecurityManager Members

        public bool ValidateUser(string userName, string password)
        {
            try
            {
                User user = SecurityDB.Instance.GetUser(userName);
                if (user != null)
                {
                    if (user.Password == password)
                    {
                        return true;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            return false;
        }

        public void CreateUser(ref User user)
        {
            //Todo
        }

        public bool ChangePasswordQuestionAndAnswer(string applicationName, string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
            /*
            SqlConnectionHolder holder = null;
            try {
                holder = SqlConnectionHelper.GetConnection( _sqlConnectionString, true );
                CheckSchemaVersion( holder.Connection );

                SqlCommand    cmd     = new SqlCommand("dbo.aspnet_Membership_ChangePasswordQuestionAndAnswer", holder.Connection);

                cmd.CommandTimeout = CommandTimeout;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(CreateInputParam("@ApplicationName", SqlDbType.NVarChar, ApplicationName));
                cmd.Parameters.Add(CreateInputParam("@UserName", SqlDbType.NVarChar, username));
                cmd.Parameters.Add(CreateInputParam("@NewPasswordQuestion", SqlDbType.NVarChar, newPasswordQuestion));
                cmd.Parameters.Add(CreateInputParam("@NewPasswordAnswer", SqlDbType.NVarChar, encodedPasswordAnswer));

                SqlParameter p = new SqlParameter("@ReturnValue", SqlDbType.Int);
                p.Direction = ParameterDirection.ReturnValue;
                cmd.Parameters.Add(p);

                cmd.ExecuteNonQuery();
                int status = ( ( p.Value != null ) ? ( ( int )p.Value ) : -1 );
                if( status != 0 )
                {
                    throw new ProviderException( GetExceptionText( status ) );
                }

                return ( status == 0 );
            }
            finally
            {
                if( holder != null )
                {
                    holder.Close();
                    holder = null;
                }
            }
            */
        }

        public bool ChangePassword(string applicationName, string username, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();


            /*
            SqlConnectionHolder holder = null;
            try {
                holder = SqlConnectionHelper.GetConnection( _sqlConnectionString, true );
                CheckSchemaVersion( holder.Connection );

                SqlCommand  cmd = new SqlCommand( "dbo.aspnet_Membership_SetPassword", holder.Connection );

                cmd.CommandTimeout = CommandTimeout;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(CreateInputParam("@ApplicationName", SqlDbType.NVarChar, ApplicationName));
                cmd.Parameters.Add(CreateInputParam("@UserName", SqlDbType.NVarChar, username));
                cmd.Parameters.Add(CreateInputParam("@NewPassword", SqlDbType.NVarChar, pass));
                cmd.Parameters.Add(CreateInputParam("@PasswordSalt", SqlDbType.NVarChar, salt));
                cmd.Parameters.Add(CreateInputParam("@PasswordFormat", SqlDbType.Int, passwordFormat));
                cmd.Parameters.Add(CreateInputParam("@CurrentTimeUtc", SqlDbType.DateTime, DateTime.UtcNow));

                SqlParameter p = new SqlParameter("@ReturnValue", SqlDbType.Int);
                p.Direction = ParameterDirection.ReturnValue;
                cmd.Parameters.Add(p);

                cmd.ExecuteNonQuery();

                status =  ( ( p.Value != null ) ? ( ( int )p.Value ) : -1 );

                if ( status != 0 )
                {
                    string errText = GetExceptionText( status );

                    if ( IsStatusDueToBadPassword( status ) )
                    {
                        throw new MembershipPasswordException( errText );
                    }
                    else
                    {
                        throw new ProviderException( errText );
                    }
                }

                return true;
            }
            finally
            {
                if( holder != null )
                {
                    holder.Close();
                    holder = null;
                }
            }
             * */
        }

        public bool ResetPassword(string username, string newpassword, string passwordAnswer)
        {
            throw new NotImplementedException();
        }

        public bool ResetPassword(string username, string newpassword)
        {
            throw new NotImplementedException();
        }

        public void UpdateUser(User modifieduser)
        {
            throw new NotImplementedException();
        }

        public bool UnlockUser(string username, string ApplicationName)
        {
            throw new NotImplementedException();
        }

        bool ISecurityMaster.ValidateUser(string userName, string password)
        {
            throw new NotImplementedException();
        }

        void ISecurityMaster.CreateUser(ref User user)
        {
            throw new NotImplementedException();
        }

        bool ISecurityMaster.ChangePasswordQuestionAndAnswer(string applicationName, string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        bool ISecurityMaster.ChangePassword(string applicationName, string username, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        bool ISecurityMaster.ResetPassword(string username, string newpassword, string passwordAnswer)
        {
            throw new NotImplementedException();
        }

        bool ISecurityMaster.ResetPassword(string username, string newpassword)
        {
            throw new NotImplementedException();
        }

        void ISecurityMaster.UpdateUser(User modifieduser)
        {
            throw new NotImplementedException();
        }

        bool ISecurityMaster.UnlockUser(string username, string ApplicationName)
        {
            throw new NotImplementedException();
        }

        public string GetUserNameByEmail(string email, string ApplicationName)
        {
            throw new NotImplementedException();
        }

        public bool DeleteUser(string username, string ApplicationName, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public int GetNumberOfUsersOnline(string ApplicationName, int p)
        {
            throw new NotImplementedException();
        }

        public User GetUser(Guid guid, string ApplicationName, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public User GetUser(string username, string ApplicationName, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public UserList GetAllUsers(string ApplicationName, int pageIndex, int pageSize)
        {
            throw new NotImplementedException();
        }

        public UserList FindUsersByName(string ApplicationName, string usernameToMatch, int pageIndex, int pageSize)
        {
            throw new NotImplementedException();
        }

        public UserList FindUsersByEmail(string ApplicationName, string emailToMatch, int pageIndex, int pageSize)
        {
            throw new NotImplementedException();
        }

        public bool IsUserInRole(string ApplicationName, string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public List<string> GetRolesForUser(string ApplicationName, string username)
        {
            throw new NotImplementedException();
        }

        public void CreateRole(string ApplicationName, string roleName)
        {
            throw new NotImplementedException();
        }

        public bool DeleteRole(string ApplicationName, string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public bool RoleExists(string ApplicationName, string roleName)
        {
            throw new NotImplementedException();
        }

        public List<string> FindUsersInRole(string ApplicationName, string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public List<string> GetAllRoles(string ApplicationName)
        {
            throw new NotImplementedException();
        }

        public List<string> GetUsersInRole(string ApplicationName, string roleName)
        {
            throw new NotImplementedException();
        }

        public void AddUsersToRoles(string ApplicationName, string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public void RemoveUsersFromRoles(string ApplicationName, string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
        
}
